package util

import (
	"archive/zip"
	"fmt"
	"image"
	"image/jpeg"
	"image/png"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"resize"
	"strings"
)

// PathExists 判断文件夹是否存在
func PathExists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return false
}

// ClassPageExists 判断班级是否存在
func ClassPageExists(class string) bool {
	imgDirPath := fmt.Sprintf("./img/%s", class)
	exportDirPath := fmt.Sprintf("./export/%s", class)
	return ClassExists(imgDirPath, exportDirPath)
}

// ClassExists 判断班级文件夹是否存在
func ClassExists(path1 string, path2 string) bool {
	isDir := PathExists(path1)
	if !isDir {
		return false
	}
	isDir = PathExists(path2)
	if isDir {
		return true
	}
	return false
}

// ListFile ..
func ListFile(myfolder string) []string {
	var fileList []string
	files, _ := ioutil.ReadDir(myfolder)
	for _, file := range files {
		fileList = append(fileList, file.Name())
	}
	return fileList
}

// Zip 压缩文件
func Zip(srcFile string, destZip string) error {
	zipfile, err := os.Create(destZip)
	if err != nil {
		return err
	}
	defer zipfile.Close()

	archive := zip.NewWriter(zipfile)
	defer archive.Close()

	filepath.Walk(srcFile, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		header, err := zip.FileInfoHeader(info)
		if err != nil {
			return err
		}

		header.Name = strings.TrimPrefix(path, filepath.Dir(srcFile)+"/")
		// header.Name = path
		if info.IsDir() {
			header.Name += "/"
		} else {
			header.Method = zip.Deflate
		}

		writer, err := archive.CreateHeader(header)
		if err != nil {
			return err
		}

		if !info.IsDir() {
			file, err := os.Open(path)
			if err != nil {
				return err
			}
			defer file.Close()
			_, err = io.Copy(writer, file)
		}
		return err
	})

	return err
}

// CompressImg  ...
func CompressImg(source string, path string) string {
	var err error
	var file *os.File

	// 打开文件
	if file, err = os.Open(source); err != nil {
		err := "can not open file"
		return err
	}
	// 函数返回时执行
	defer file.Close()

	// 文件名
	name := file.Name()

	// 图片对象
	var img image.Image
	switch {
	case strings.HasSuffix(name, ".png"):
		if img, err = png.Decode(file); err != nil {
			err := fmt.Sprintf("打开文件失败")
			return err
		}
	case strings.HasSuffix(name, ".PNG"):
		if img, err = png.Decode(file); err != nil {
			err := fmt.Sprintf("格式错误")
			return err
		}
	case strings.HasSuffix(name, ".jpg"):
		if img, err = jpeg.Decode(file); err != nil {
			err := fmt.Sprintf("格式错误")
			return err
		}
	case strings.HasSuffix(name, ".JPG"):
		if img, err = jpeg.Decode(file); err != nil {
			err := fmt.Sprintf("格式错误")
			return err
		}
	default:
		err := fmt.Sprintf("格式错误")
		return err
	}

	resizeImg := resize.Resize(400, 0, img, resize.NearestNeighbor)

	newName := path
	if outFile, err := os.Create(newName); err != nil {
		err := fmt.Sprintf("无法写入")
		return err
	} else {
		defer outFile.Close()
		err = jpeg.Encode(outFile, resizeImg, nil)
		if err != nil {
			err := fmt.Sprintf("错误")
			return err
		}
	}
	return "压缩成功"
}
